import pandas as pd
import numpy as np
data_array = np.array([
    ['Alice', 25, 'New York', 'Female', 88.5],
    ['Bob', 30, 'Los Angeles', 'Male', 92.0],
    ['Charlie', 35, 'Chicago', 'Male', 78.5]
])
df = pd.DataFrame(data_array, columns=['Name', 'Age', 'City', 'Gender', 'Score'])
# 将 'Age' 列转换为整数类型
df['Age'] = pd.to_numeric(df['Age'], errors='coerce')
# 按城市分组计算平均年龄
grouped = df.groupby('City')['Age'].mean()
print(grouped)
print('-'*35)

# 多级分组与聚合
groupby = df.groupby(['City', 'Gender']).agg({'Age': ['mean', 'max'], 'Score': 'sum'})
print(groupby)


